package com.leetroll.media.job;

import com.leetroll.api.service.ItemService;
import com.leetroll.common.entity.Item;
import com.leetroll.common.enums.EFileType;
import com.leetroll.common.enums.EItemType;
import com.leetroll.media.service.JobExecutor;
import com.leetroll.media.service.UpDownloadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.concurrent.ExecutionException;

@Service
@Slf4j
public class Job_Sound01 {

    private final ItemService itemService;
    private final UpDownloadService upDownloadService;
    private JobExecutor jobExecutor;

    public Job_Sound01(ItemService itemService, UpDownloadService upDownloadService, JobExecutor jobExecutor) {
        this.itemService = itemService;
        this.upDownloadService = upDownloadService;
        this.jobExecutor = jobExecutor;
    }

    public void upload() throws ExecutionException, InterruptedException {
        //F:\_resources\sound\AVA INSTINCT Trailer Sound Effects\Samples

        File folder = new File("F:\\_resources\\sound\\AYN Sounds - All You Need Vol. 1\\Samples");
        if (!folder.exists() || !folder.isDirectory()) return;

        File[] soundFiles = folder.listFiles();
        if (soundFiles == null) return;

        for (File soundFolder : soundFiles) {
            jobExecutor.submit(() -> uploadItem_02(soundFolder));
//            for (File file : soundFolder.listFiles()) {
//                jobExecutor.submit(() -> uploadItem_02(file));
//            }
        }
        jobExecutor.await();
    }

    private Integer uploadItem_02(File soundFile) {
        if (soundFile.getName().endsWith("pdf")) return 0;

        String name = soundFile.getName().split("\\.")[0];
        log.info(">>>>>>>>>>>>>>>> 准备插入: {}", name);

//        String metric = name.substring(0, name.indexOf("-")).trim();
        //插入item表
        Item item = new Item();
        item.setName(name);
        item.setAlias(name);
        item.setCatId(5L);
        item.setStatus("show");
        item.setSource("AYN Sounds");
        item.setAuthor("MilkShark");
        item.setPrice(0.0);
        item.setMark("");
        item.setType(EItemType.SOUND.getType());
        item.setDesp("");
        item.setTag("Samples");
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);

        //插入主图文件
        upDownloadService.upload2AliByType(item.getId(), soundFile, EFileType.MAIN);
        log.info("\t\t\t>> 音效插入成功");

        return 1;
    }

    private Integer uploadItem_01(File soundFile) {
        String fileName = soundFile.getName();
        String name = fileName.substring(fileName.indexOf("Instinct - ") + "Instinct - ".length()).split("\\.")[0];
        log.info(">>>>>>>>>>>>>>>> 准备插入: {}", name);

        String metric = name.substring(0, name.indexOf("-")).trim();
        //插入item表
        Item item = new Item();
        item.setName(name);
        item.setAlias(name);
        item.setCatId(5L);
        item.setStatus("show");
        item.setSource("AVA INSTINCT Trailer Sound Effects");
        item.setAuthor("绝望的眼镜蛇");
        item.setPrice(0.0);
        item.setMark("");
        item.setType(EItemType.SOUND.getType());
        item.setDesp("");
        item.setTag(metric);
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
//        itemService.save(item);

        //插入主图文件
//        upDownloadService.upload2AliByType(item.getId(), soundFile, EFileType.MAIN);
        log.info("\t\t\t>> 音效插入成功");

        return 1;
    }

}
